"""
Case Type   : 资源池化集群DMS reform优化
Case Name   : 踢出主机->reform在recovery阶段kill备1、备2
Create At   : 2024/05/13
Owner       : @kyleze
Description :
    1.踢出主机
    2.持续观察DMS日志
    3.kill备1、备2
    4.拉起原主节点
    5.查询集群状态
Expect      :
    1.成功
    2.reform进入frecovery阶段
    3.成功
    4.成功
    5.集群恢复正常
History     :
"""

import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


class DmsRform(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.dirname(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.pri_user_node = Node(node="PrimaryDbUser")
        self.st1_user_node = Node(node="Standby1DbUser")
        self.st2_user_node = Node(node="Standby2DbUser")
        self.dms_log_file = os.path.join('$GAUSSLOG', 'pg_log/DMS/run/dms.rlog')
        self.reform_start = int(self.pri_user_node.sh(
            f'source {macro.DB_ENV_PATH};grep "dms_reform_set_start" '
            f'{self.dms_log_file}|wc -l').result())
        self.flush_copy = int(self.pri_user_node.sh(
            f'source {macro.DB_ENV_PATH};'
            f'grep "dms_reform_flush_copy_parallel enter" '
            f'{self.dms_log_file}|wc -l').result())

    def test_dms(self):
        step = '--step1:踢出主机 expect:成功--'
        self.log.info(step)
        stop_res = self.primary_sh.exec_cm_ctl("stop", '-n 1')
        self.log.info(stop_res)
        self.assertIn(self.constant.cm_stop_node_success_msg, stop_res,
                      "执行失败" + step)

        step = '--step2:持续观察DMS日志 expect:reform进入frecovery阶段--'
        self.log.info(step)
        reform_flush_num = 0
        for i in range(10):
            reform_start_num = int(
                self.st1_user_node.sh(f'grep "dms_reform_set_start" '
                                      f'{self.dms_log_file}|wc -l').result())
            reform_flush_num = int(self.st1_user_node.sh(
                f'grep "dms_reform_recovery_opengauss enter" '
                f'{self.dms_log_file}|wc -l').result())
            if reform_start_num > self.reform_start \
                    and reform_flush_num > self.flush_copy:
                break
        self.assertTrue(reform_flush_num > self.flush_copy, "执行失败" + step)

        step = '--step3:kill备1 expect:成功--'
        self.log.info(step)
        kill_cmd = "ps ux | grep '\-D' | grep gaussdb | grep -v grep | " \
                   "awk '{print $2}' | xargs kill -9"
        st1_kill_res = self.st1_user_node.sh(kill_cmd).result()
        self.log.info(st1_kill_res)
        self.assertIn('', st1_kill_res, "执行失败" + step)
        st2_kill_res = self.st2_user_node.sh(kill_cmd).result()
        self.log.info(st2_kill_res)
        self.assertIn('', st2_kill_res, "执行失败" + step)

        step = '--step4:拉起原主节点 expect:成功--'
        self.log.info(step)
        stop_res = self.primary_sh.exec_cm_ctl("start", '-n 1')
        self.log.info(stop_res)
        self.assertIn(self.constant.cm_start_node_success_msg, stop_res,
                      "执行失败" + step)

        step = '--step5:查看集群状态 expect:集群状态正常--'
        self.log.info(step)
        time.sleep(20)
        status = self.primary_sh.get_db_cluster_status('status')
        self.assertTrue("Degraded" in status, "执行失败" + step)

    def tearDown(self):
        self.log.info('--恢复环境--')
        switchover_res = self.primary_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(switchover_res)
        self.assertIn(self.constant.cm_switchover_success_msg,
                      switchover_res, "恢复环境失败")
        self.log.info(f'----{os.path.dirname(__file__)} end----')
